package com.jyy.prefabricated.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import com.jyy.prefabricated.common.AjaxResult;
import com.jyy.prefabricated.dto.BatchDto;
import com.jyy.prefabricated.dto.Pageable;
import com.jyy.prefabricated.entity.LevelEvaluation;
import com.jyy.prefabricated.service.LevelEvaluationService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 水平评价
 * @version 1.0.0
 * @author: zjj
 */
@Slf4j
@RequestMapping("/api")
@RestController
public class LevelEvaluationController {

    @Autowired
    private LevelEvaluationService levelEvaluationService;

    /**
     * 新增水平评价
     * @param levelEvaluation
     * @return
     */
    @PostMapping("/level-evaluation")
    public AjaxResult insert(@Valid @RequestBody LevelEvaluation levelEvaluation){
        log.info("Rest request to insert levelEvaluation:{}",levelEvaluation);
        int id = levelEvaluationService.insert(levelEvaluation);
        LevelEvaluation result = levelEvaluationService.selectById(id);
        return AjaxResult.success(result);
    }

    /**
     * 更新水平评价
     * @param levelEvaluation
     * @return
     */
    @PutMapping("/level-evaluation")
    public AjaxResult update(@Valid @RequestBody LevelEvaluation levelEvaluation){
        log.info("Rest request to update levelEvaluation:{}",levelEvaluation);
        levelEvaluationService.update(levelEvaluation);
        LevelEvaluation result = levelEvaluationService.selectById(levelEvaluation.getId());
        return AjaxResult.success(result);
    }

    /**
     * 批量更新状态
     * @param dto
     * @return
     */
    @PutMapping("/level-evaluation-batch")
    public AjaxResult update(@Valid @RequestBody BatchDto dto){
        log.info("Rest request to batch-update levelEvaluation:{}",dto);
        levelEvaluationService.batchUpdateStatus(dto);
        return AjaxResult.success();
    }

    /**
     * 批量删除
     * @param dto
     * @return
     */
    @DeleteMapping("/level-evaluation-batch")
    public AjaxResult deleteById(@RequestBody BatchDto dto){
        log.info("Rest request to batch-delete levelEvaluation by id:{}",dto);
        int row = levelEvaluationService.batchDelete(dto);
        if(row > 0){
            return AjaxResult.success();
        } else {
            return AjaxResult.error();
        }
    }

    /**
     * 查询评价详情
     * @param id
     * @return
     */
    @GetMapping("/level-evaluation/{id}")
    public AjaxResult selectById(@PathVariable Integer id){
        log.info("Rest request to find levelEvaluation by id:{}",id);
        LevelEvaluation result = levelEvaluationService.selectById(id);
        return AjaxResult.success(result);
    }

    /**
     * 删除评价
     * @param id
     * @return
     */
    @DeleteMapping("/level-evaluation/{id}")
    public AjaxResult deleteById(@PathVariable Integer id){
        log.info("Rest request to delete levelEvaluation by id:{}",id);
        int row = levelEvaluationService.delete(id);
        if(row > 0){
            return AjaxResult.success();
        } else {
            return AjaxResult.error();
        }
    }

    /**
     * 查询上一条评价
     * @param id
     * @return
     */
    @GetMapping("/level-evaluation-prev/{id}")
    public AjaxResult selectByPrev(@PathVariable Integer id){
        log.info("Rest request to find levelEvaluation by prev id:{}",id);
        List<LevelEvaluation> result = levelEvaluationService.selectByPage(id,"prev");
        return AjaxResult.success(result);
    }

    /**
     * 查询下一条评价
     * @param id
     * @return
     */
    @GetMapping("/level-evaluation-next/{id}")
    public AjaxResult selectByNext(@PathVariable Integer id){
        log.info("Rest request to find levelEvaluation by next id:{}",id);
        List<LevelEvaluation> result = levelEvaluationService.selectByPage(id,"next");
        return AjaxResult.success(result);
    }

    /**
     * 分页查询评价
     * @param categoryId 分类id
     * @param title 标题
     * @param status 状态
     * @param pageable 分页信息
     * @return
     */
    @GetMapping("/level-evaluation")
    public AjaxResult selectByNext(@RequestParam(required = false) Integer categoryId, @RequestParam(required = false, defaultValue = "") String title, @RequestParam(required = false) Integer status, Pageable pageable){
        log.info("Rest request to find levelEvaluation by params categoryId:{}, title:{}, status:{}",categoryId, title, status);
        //List<Integer> categoryIds = levelEvaluationService.selectDeepCategoryIds(categoryId);
        List<Integer> categoryIds = Lists.newArrayList();
        if (categoryId != null) {
            categoryIds.add(categoryId);
        }
        PageHelper.startPage(pageable.getPageNum(), pageable.getPageSize());
        List<LevelEvaluation> levelEvaluation = levelEvaluationService.selectDeepByParams(categoryIds, title, status);
        PageInfo<LevelEvaluation> result = new PageInfo<>(levelEvaluation);
        return AjaxResult.success(result);
    }

    /**
     * 查询评价
     * @param categoryId 分类id
     * @param title 标题
     * @param status 状态
     * @return
     */
    @GetMapping("/level-evaluation-list")
    public AjaxResult selectByParams(@RequestParam(required = false) Integer categoryId, @RequestParam(required = false, defaultValue = "") String title, @RequestParam(required = false) Integer status){
        log.info("Rest request to find levelEvaluation by params categoryId:{}, title:{}, status:{}",categoryId, title, status);
        List<Integer> categoryIds = levelEvaluationService.selectDeepCategoryIds(categoryId);
        List<LevelEvaluation> result = levelEvaluationService.selectDeepByParams(categoryIds, title, status);
        return AjaxResult.success(result);
    }
}
